<script lang='ts' setup>
import { ref, watch } from 'vue'
import MenuItemCom from '@/components/MenuItemCom.vue'
import { useRoute, useRouter } from 'vue-router'
import routes from '@/router'
import { getLocalRoute } from '@/utils/commonFunction'
import { ElMessageBox } from "element-plus";
import { logoutSystem } from "@/utils/user";

const route = useRoute()
const router = useRouter()
const isCollapseMenu = ref(false)
const isLoginStatus=ref(false)

const appRouter = () => {
  let r = transformTree(getLocalRoute().filter((item: any) => item.meta.hide !== true));
  return r
}

function handleLogout() {
  ElMessageBox.confirm('确定退出登录吗？', '提示', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
  })
    .then(() => {
      logoutSystem()
      getIsLogin()
    }).catch(() => {
    });
}

interface InputNode {
  id: number;
  label: string;
  fullPath: string;
  url: string;
  icon?: string;
  children?: InputNode[];
  pageData?: any;
  type: number;
  isCustom?: boolean;
  pageFile?: string;
  hide?: boolean;

}

interface OutputNode {
  id: number;
  path: string;
  name: string;
  meta: {
    title: string;
    icon?: string;
    hide: boolean;
    type: number;
    isCustom?: boolean;
    pageFile?: string;

  };
  children: OutputNode[];
}

function transformTree(nodes: InputNode[]): OutputNode[] {

  return nodes.map((node: any) => {
    const transformedNode: OutputNode = {
      id: node.id,
      path: node.path || '/',
      name: node.url || '',
      meta: {
        title: node.label || '',
        icon: node?.icon,
        hide: node?.hide || false,   // 固定值
        type: node.type,
        isCustom: node.type == '3',//自定义页面
        pageFile: node.data || ''
      },
      children: node.children ? transformTree(node.children) : [],
    };
    return transformedNode;
  });
}

function getIsLogin() {
  isLoginStatus.value =  localStorage.getItem('userInfo')!=null;
}
getIsLogin()
function handleLogin(){
  router.push('/login')
}

// watch(()=>route,()=>{
//     console.log(appRouter());
// },{immediate:true})

</script>
<template>
  <div class="el-menu-container">
    <div class="topMenu">
      <el-menu router :default-active="route.path" :collapse="isCollapseMenu" mode="horizontal">
        <menu-item-com :menuItem="item" v-for="item in appRouter()" />
      </el-menu>
      <el-button class="logoutBtn" link @click="handleLogout" v-if="isLoginStatus">退出登录</el-button>
      <el-button class="logoutBtn" link @click="handleLogin" v-else>登录</el-button>
    </div>

    <div class="pageMain">
      <div class="pageView">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>
<style scoped lang='scss'>
.topMenu {
  position: relative;
  box-shadow: 0 3px 8px 0px #abaaaa5c;
  display: flex;
  justify-content: center;

  :deep(.el-menu--horizontal) {
    justify-content: center;
    width: 100%;
  }
}

.pageView {
  background-color: white;
  overflow: auto;
  box-sizing: border-box;
}

.pageMain {
  background-color: #f1f1f1;
  overflow: auto;
  box-sizing: border-box;
}

.pageHeader {
  border-bottom: 1px solid #ccc;
  position: fixed;
  background-color: #429dff;
  top: 0;
  left: 0;
  z-index: var(--max-index);
  width: 100vw;



}

.logoutBtn {
  position: absolute;
  right: 50px;
  top: 20px;
}

.el-menu-container {
  position: fixed;
  left: 0;
  overflow: auto;
  background-color: #fff;
  scrollbar-width: thin;
  z-index: var(--max-index);
  transition: all 0.3s ease-in-out;

  :deep(.el-menu) {
    border-right: none !important;
  }
}
</style>